package com.housediyadmin.view.components
{
	/**
	 * 
	 * @author wenjun cai
	 * 2010/11/24
	 */	
	import com.housediyadmin.events.SelectedFittingEve;
	import com.housediyadmin.events.brandEvents.GetBrandEvent;
	import com.housediyadmin.events.fittingEvents.GetFittingEvent;
	import com.housediyadmin.events.goodsEvents.DeleteGoodsByIDsEvent;
	import com.housediyadmin.events.goodsEvents.EditGoodsEvent;
	import com.housediyadmin.events.goodsEvents.GetGoodsByBrandIDEvent;
	import com.housediyadmin.model.BrandModel;
	import com.housediyadmin.model.FittingModel;
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.model.GoodsTreeModel;
	import com.housediyadmin.view.titlewindow.EditGoodsView;
	import com.housediyadmin.vo.Brand;
	import com.housediyadmin.vo.Fitting;
	import com.housediyadmin.vo.Goods;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.DateField;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.CloseEvent;
	import mx.events.IndexChangedEvent;
	import mx.events.ListEvent;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	public class RentererGoodsMediator extends Mediator
	{
		[Inject]
		public var view:RentererGoodsView;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		[Inject]
		public var goodsTreeModel:GoodsTreeModel;
		
		[Inject]
		public var fittingModel:FittingModel;
		
		[Inject]
		public var brandModel:BrandModel;
		
		private var oldGoods:Goods;
		private var newGoods:Goods;
	
		public function RentererGoodsMediator()
		{
			super();
		}
		
		override public function onRegister():void
		{
			
			newGoods=new Goods();
			
			view.modifyBtn.addEventListener(MouseEvent.CLICK, modifyEve);		
			view.delBtn.addEventListener(MouseEvent.CLICK,deleteEve);
			view.selectedCheckBox.addEventListener(MouseEvent.CLICK,checkBoxSelected);
		}
	
		private function modifyEve(e:MouseEvent):void
		{
			var popup:EditGoodsView = new EditGoodsView();
			popup.type="EDIT";
			mediatorMap.createMediator(popup);
			PopUpManager.addPopUp( popup, contextView,true ); 
			PopUpManager.centerPopUp(popup);
		}
		private function deleteEve(e:MouseEvent):void
		{
			oldGoods=goodsModel.selectedGoods;
			Alert.yesLabel="是";
			Alert.noLabel="否";
			Alert.cancelLabel="取消";
			Alert.show("删除商品信息吗？","",3,view,deleteAlert);
		}
		
		private function deleteAlert(e:CloseEvent):void
		{
			if(e.detail==Alert.YES)
			{
				var goodsIds:Array=new Array();
				goodsIds.push(goodsModel.selectedGoods.goodsID);
				
				var typeIds:Array=new Array();
				var typeID:int=goodsTreeModel.selectedNode.@typeID;
				typeIds.push(typeID);
				dispatch(new DeleteGoodsByIDsEvent(DeleteGoodsByIDsEvent.DELETE_GOODS_BY_IDS,goodsIds,typeIds));
//				for each(var goods:Goods in goodsModel.goodsArray)
//				{
//					if(goods)
//					{
//						goods.isSelected=true;
//					}
//				}
			}
		}
		private function checkBoxSelected(e:MouseEvent):void
		{
			oldGoods=goodsModel.selectedGoods;
			if(e.target.selected)
			{
				goodsModel.selectedGoodsArr.push(goodsModel.selectedGoods);
				/*goodsModel.selectedGoods.isSelected=true;*/
			}
			else
			{
				var index:int=goodsModel.selectedGoodsArr.indexOf(goodsModel.selectedGoods);
				goodsModel.selectedGoodsArr.splice(index,1);
				/*goodsModel.selectedGoods.isSelected=false;*/
			}
		}
	}
}